<?php
//代理商结算管理
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
//展示页
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array();
	//查询
	//代理商id
     if(!empty($_RQ['uniacid'])){
		$params['uniacid']=trim($_RQ['uniacid']);
		$return['params']['uniacid']=$params['uniacid'];
	}
	//审核状态
	if(!empty($_RQ['status'])){
		$params['status']=trim($_RQ['status']);
		$return['params']['status']=$params['status'];
	}
	//打款状态
	if(!empty($_RQ['ispay'])){
		$params['ispay']=trim($_RQ['ispay']);
		$return['params']['ispay']=$params['ispay'];
	}
	//支付订单
	if(!empty($_RQ['trade_id'])){
		$params['trade_id']=trim($_RQ['trade_id']);
		$return['params']['trade_id']=$params['trade_id'];
	}
	$fields=array('id','uniacid','ordersn','money','status','ispay','createtime','pay_type','trade_id');
    $orderby='id desc';
	$total='';
	//$parameter=array('params'=>$params,'limit'=>array($pindex,$psize),'fields'=>array('id','uniacid','order_id','money','status','clearsn','createtime'));
	$list = pdo_getslice('core_agent_clear',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if($list){
		foreach($list as &$item){

		}
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['list']=$list?$list:array();
	$htmls=array(array('type' =>'list','tabs'=>array(
			array('op'=>'list','title'=>'代理商结算管理'),
			),
			'list' =>array ('data' => 'list','list' => array (0 => array (
		        array('title' => 'id','name' => 'id', 'type' => 'string',),),
		        array ( array ('title' => '代理商ID','name' => 'uniacid','type' => 'string',),),
		         array (array ('title' => '结算单号','name' => 'ordersn','type' => 'string',),),
				array(array('title' => '结算金额','name' => 'money','type' => 'string',),),
		         array (array ('title' => '审核状态','name' => 'status','type' => 'radio','options' => 'status_xuanxiang',), ),
		         array (array ('title' => '打款状态','name' => 'ispay','type' => 'radio','options' => 'ispay_xuanxiang',), ),
		         array (array ('title' => '支付方式','name' => 'pay_type','type' => 'radio','options' => 'pay_type_xuanxiang',), ),
				 array (array ('title' => '支付单号','name' => 'trade_id','type' => 'string', ),),
				array (array ('title' => '结算时间','name' => 'createtime','type' => 'time', ),),
				),
			   'delall' => true,
		       'del_one' => true,
		       'edit' => false,
		       'action'=>array(
			   array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>'0')),
			   array('type'=>'modal','op'=>'balance','title'=>'打款','show'=>array('ispay'=>'0')),
				   ),),
		'search'=>array(
		  array('type'=>'string','width'=>15,'title'=>'代理商ID','name' => 'uniacid'),
		  array('type'=>'radio','width'=>15,'title'=>'审核状态','name' => 'status','options'=>'status_xuanxiang'),
		  array('type'=>'radio','width'=>15,'title'=>'打款状态','name' => 'ispay','options'=>'ispay_xuanxiang'),
		  array('type'=>'string','width'=>15,'title'=>'支付订单','name' => 'trade_id'),
		  ),
		  ),
	     );
	$return['htmls']=$htmls;
	$return['status_xuanxiang']=array (0 => '待审核',1 => '审核通过');
	$return['ispay_xuanxiang']=array (0 => '待申请',1 => '等待打款',2=>'打款完成');
	$return['pay_type_xuanxiang']=array (0 => '支付宝支付',1 => '微信支付',2=>'银联支付');
	exi($return);
}
if($_RQ['op']=='del'){
	
	$id=intval($_RQ['id']);

	if($id){
		pdo_delete('core_agent_clear',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_agent_clear',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
  }
if($_RQ['op']=='check'){
	//print_r($_RQ);exit();
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('core_agent_clear',array('id'=>$id));
	}else{
		exi('错误id','error');
	}
    $return['item']=$item;
	$return['check']=array('1'=>'审核通过');
	$htmls=array(
		'type'=>'edit',
		'post'=>'check2',
		'desc'=>'审核',
		'edit'=>array(
			'1'=>array('name'=>'status','title'=>'审核','type'=>'radio','options'=>'check'),
			)
			);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='check2'){
	$postdata=get_postdata();
	$data=array_fields(array('status'),$postdata);
	$id=intval($postdata['id']);
	//$data['checktime']=TIMESTAMP;
	if($id){
		pdo_update('core_agent_clear',$data,array('id'=>$id));
		exi('审核成功！');
	}
}
if($_RQ['op']=='balance'){
	//print_r($_RQ);exit();
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('core_agent_clear',array('id'=>$id));
	}else{
		exi('错误id','error');
	}
    $return['item']=$item;
	$return['pay_type']=array('0'=>'支付宝支付','1'=>'微信支付','2'=>'银联支付');
	$htmls=array(
		'type'=>'edit',
		'post'=>'balance2',
		'desc'=>'打款审核',
		'edit'=>array(
			'1'=>array('name'=>'pay_type','title'=>'打款方式','type'=>'radio','options'=>'pay_type'),
			'2'=>array('name'=>'ordersn','title'=>'打款订单号','type'=>'string'),
			)
			);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='balance2'){
	$postdata=get_postdata();
	$data=array_fields(array('pay_type','ordersn'),$postdata);
	$id=intval($postdata['id']);
	//$data['checktime']=TIMESTAMP;
	if($id){
		pdo_update('core_agent_clear',$data,array('id'=>$id));
		exi('打款申请成功！');
	}
	else{
		
		exi('打款申请失败,请重试','error');
	}
}